<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="author" content="GoJun" />
	
	
	
	<title>如何设置 BottomSheetDialog 高度 ｜ GoJun</title>
	
    
    
    <meta name="description" content="BottomSheetDialog 控制初始高度是跟 behavior_peekHeight 属性有关 样式主题设置 &amp;lt;style name=&amp;#34;BottomSheetDialog&amp;#34; parent=&amp;#34;Theme.Design.Light.BottomSheetDialog&amp;#34;&amp;gt; &amp;lt;item name=&amp;#34;bottomSheetStyle&amp;#34;&amp;gt;@style/bottomSheetStyleWrapper&amp;lt;/item&amp;gt; &amp;lt;/style&amp;gt; &amp;lt;style name=&amp;#34;bottomSheetStyleWrapper&amp;#34; parent=&amp;#34;Widget.Design.BottomSheet.Modal&amp;#34;&amp;gt; &amp;lt;item name=&amp;#34;behavior_peekHeight&amp;#34;&amp;gt;500dp&amp;lt;/item&amp;gt; &amp;lt;/style&amp;gt; &amp;lt;!--代码中应用--&amp;gt; BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.BottomSheetDialog); dialog.setContentView(R.layout.layout_bottom_sheet); dialog.show(); 动态代码设置 Dialog 的布局最外层使用 RelativeLayout 或者 ConstraintLayout, 其他控件设置高度无法生效 需要在子内容布局设置高" />
    

    
    
    <meta name="keywords" content="Hugo, theme, zozo" />
    

	
    
    <link rel="shortcut icon" href="https://gojun.me/images/favicon.ico" />

    <link rel="stylesheet" type="text/css" media="screen" href="https://gojun.mecss/normalize.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="https://cdn.jsdelivr.net/npm/animate.css@4.1.0/animate.min.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="https://gojun.me/css/zozo.css" />
	<link rel="stylesheet" type="text/css" media="screen" href="https://cdn.jsdelivr.net/npm/remixicon@2.5.0/fonts/remixicon.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="https://gojun.me/css/highlight.css" />

    
    
</head>

<body>
    <div class="main animate__animated animate__fadeInDown">
        <div class="nav_container animated fadeInDown">
    <div class="site_nav" id="site_nav">
        <ul>
            
            <li>
                <a href="/">Home</a>
            </li>
            
            <li>
                <a href="/posts/">Archive</a>
            </li>
            
            <li>
                <a href="/tags/">Tags</a>
            </li>
            
            <li>
                <a href="/projects/">Projects</a>
            </li>
            
        </ul>
    </div>
    <div class="menu_icon">
        <a id="menu_icon"><i class="ri-menu-line"></i></a>
    </div>
</div>
        <div class="header animated fadeInDown">
    <div class="site_title_container">
        <div class="site_title">
            <h1>
                <a href="https://gojun.me">
                    <span>GoJun</span>
                </a>
            </h1>
        </div>
        <div class="description">
            <p class="sub_title">为向往生活努力奋斗！</p>
            <div class="my_socials">
                
                
                <a href="https://github.com/freelander" title="github" target="_blank"><i class="ri-github-fill"></i></a>
                
                
                <a href="https://gojun.me/index.xml" type="application/rss+xml" title="rss" target="_blank"><i
                        class="ri-rss-fill"></i></a>
            </div>
        </div>
    </div>
</div>
        <div class="content">
            <div class="post_page">
                <div class="post animate__animated animate__fadeInDown">
                    <div class="post_title post_detail_title">
                        <h2><a href='/posts/bottom-sheet-dialog-height/'>如何设置 BottomSheetDialog 高度</a></h2>
                        <span class="date">2017.02.18</span>
                    </div>
                    <div class="post_content markdown"><p>BottomSheetDialog 控制初始高度是跟 behavior_peekHeight 属性有关</p>
<h3 id="样式主题设置">样式主题设置</h3>
<div class="highlight"><pre class="chroma"><code class="language-fallback" data-lang="fallback">&lt;style name=&#34;BottomSheetDialog&#34; parent=&#34;Theme.Design.Light.BottomSheetDialog&#34;&gt;
    &lt;item name=&#34;bottomSheetStyle&#34;&gt;@style/bottomSheetStyleWrapper&lt;/item&gt;
&lt;/style&gt;

&lt;style name=&#34;bottomSheetStyleWrapper&#34; parent=&#34;Widget.Design.BottomSheet.Modal&#34;&gt;
    &lt;item name=&#34;behavior_peekHeight&#34;&gt;500dp&lt;/item&gt;
&lt;/style&gt;

&lt;!--代码中应用--&gt;
BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.BottomSheetDialog);
            dialog.setContentView(R.layout.layout_bottom_sheet);
            dialog.show();
</code></pre></div><h3 id="动态代码设置">动态代码设置</h3>
<ol>
<li>Dialog 的布局最外层使用 RelativeLayout 或者 ConstraintLayout, 其他控件设置高度无法生效</li>
<li>需要在子内容布局设置高度 match_parent；</li>
</ol>
<div class="highlight"><pre class="chroma"><code class="language-fallback" data-lang="fallback">&lt;RelativeLayout xmlns:android=&#34;http://schemas.android.com/apk/res/android&#34;
    xmlns:app=&#34;http://schemas.android.com/apk/res-auto&#34;
    android:id=&#34;@+id/rl_container&#34;
    android:orientation=&#34;vertical&#34;
    android:layout_width=&#34;match_parent&#34;
    android:layout_height=&#34;wrap_content&#34;
    android:background=&#34;@color/text_white&#34;&gt;

    &lt;LinearLayout
        android:id=&#34;@+id/ll_address_top&#34;
        android:layout_width=&#34;match_parent&#34;
        android:layout_height=&#34;match_parent&#34;
        android:minHeight=&#34;?attr/actionBarSize&#34;
        android:orientation=&#34;horizontal&#34;&gt;
        
    &lt;/LinearLayout&gt;

&lt;/RelativeLayout&gt;

</code></pre></div><ol start="3">
<li>设置高度的代码</li>
</ol>
<div class="highlight"><pre class="chroma"><code class="language-fallback" data-lang="fallback">View parent = (View) mContentView.getParent();
if (parent != null) {
    BottomSheetBehavior mBehavior = BottomSheetBehavior.from(parent);
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    int screenHeight = displayMetrics.heightPixels;
    mBehavior.setPeekHeight((int) (screenHeight * 0.45));
    parent.setBackgroundColor(Color.TRANSPARENT);
}
</code></pre></div><h3 id="简单粗暴直接给父布局一个高度">简单粗暴直接给父布局一个高度</h3>
<div class="highlight"><pre class="chroma"><code class="language-fallback" data-lang="fallback">&lt;RelativeLayout xmlns:android=&#34;http://schemas.android.com/apk/res/android&#34;
    xmlns:app=&#34;http://schemas.android.com/apk/res-auto&#34;
    android:id=&#34;@+id/rl_container&#34;
    android:orientation=&#34;vertical&#34;
    android:layout_width=&#34;match_parent&#34;
    android:layout_height=&#34;wrap_content&#34;
    android:minHeight=&#34;300dp&#34;
    android:background=&#34;@color/text_white&#34;&gt;

    &lt;LinearLayout
        android:id=&#34;@+id/ll_address_top&#34;
        android:layout_width=&#34;match_parent&#34;
        android:layout_height=&#34;wrap_content&#34;
        android:minHeight=&#34;?attr/actionBarSize&#34;
        android:orientation=&#34;horizontal&#34;&gt;
        
    &lt;/LinearLayout&gt;

&lt;/RelativeLayout&gt;
</code></pre></div></div>
                    <div class="post_footer">
                        
                        <div class="meta">
                            <div class="info">
                                <span class="field tags">
                                    <i class="ri-stack-line"></i>
                                    
                                    <a href="https://gojun.me/tags/android/">Android</a>
                                    
                                </span>
                            </div>
                        </div>
                        
                    </div>
                </div>
                
                
                <div class="doc_comments"></div>
                
            </div>
        </div>
    </div>
    <a id="back_to_top" href="#" class="back_to_top"><i class="ri-arrow-up-s-line"></i></a>
    <footer class="footer">
    <div class="footer_slogan">
        <span></span>
    </div>
</footer>
    <script src="https://gojun.me/js/jquery-3.5.1.min.js"></script>
<link href="https://gojun.me/css/fancybox.min.css" rel="stylesheet">
<script src="https://gojun.me/js/fancybox.min.js"></script>
<script src="https://gojun.me/js/zozo.js"></script>


<script type="text/javascript" async
    src="https://cdn.bootcss.com/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
        MathJax.Hub.Config({
            tex2jax: {
                inlineMath: [['$', '$'], ['\\(', '\\)']],
                displayMath: [['$$', '$$'], ['\[\[', '\]\]']],
                processEscapes: true,
                processEnvironments: true,
                skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
                TeX: {
                    equationNumbers: { autoNumber: "AMS" },
                    extensions: ["AMSmath.js", "AMSsymbols.js"]
                }
            }
        });

        MathJax.Hub.Queue(function () {
            
            
            
            var all = MathJax.Hub.getAllJax(), i;
            for (i = 0; i < all.length; i += 1) {
                all[i].SourceElement().parentNode.className += ' has-jax';
            }
        });
    </script>

<style>
    code.has-jax {
        font: inherit;
        font-size: 100%;
        background: inherit;
        border: inherit;
        color: #515151;
    }
</style>



<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
	window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
	ga('create', 'UA-142037913-1', 'auto');
	
	ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>

</body>

</html>